package cn.edu.hunau.oop.day09;

import cn.edu.hunau.oop.day07.Apple;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.OptionalDouble;
import java.util.stream.Collectors;

/**
 * @author hb
 * @date 2025-08-14 10:25:04
 */
public class TestStream {

    @Test
    public void test01(){
        //获得所有的红苹果
        List<Apple> appleList=getApplies();
        List<Apple> redApplies = appleList.stream()
                .filter(a -> "红色".equals(a.getColor()))
                .toList();
        System.out.println(redApplies);
        //获得所有红苹果的重量
        double sum = appleList.stream()
                .filter(a -> "红色".equals(a.getColor()))
                .mapToDouble(a -> a.getWeight()).sum();
        System.out.println(sum);


       // 获得国光品种红苹果的平均重量
        OptionalDouble optional = appleList.stream()
                .filter(a -> "国光".equals(a.getType()))
                .filter(a -> "红色".equals(a.getColor()))
                .mapToDouble(a -> a.getWeight())
                .average();
        System.out.println(optional.getAsDouble());


    }

    @Test
    public void test02(){
        /*
        Map<String,List<Double>> weightGroup=new HashMap<>();
        for (Apple apple : datas){
            String type=apple.getType();
            List<Double> weightList=weightGroup.getOrDefault(type,new ArrayList<>());
            weightList.add(apple.getWeight());
            weightGroup.put(type,weightList);
        }
        */
        List<Apple> datas = getApplies();
        //根据苹果的品种进行分组
        //Map<String, List<Apple>> m1 = datas.stream().collect(Collectors.groupingBy(a -> a.getType()));
        Map<String, List<Apple>> m1 = datas.stream().collect(Collectors.groupingBy(Apple::getType));
        System.out.println(m1);

        Map<String, List<Double>> m2 = datas.stream()
                .collect(Collectors.groupingBy(Apple::getType, Collectors.mapping(Apple::getWeight, Collectors.toList())));
        System.out.println(m2);
    }


    public List<Apple> getApplies(){
        List<Apple> appleList=new ArrayList<>();
        // 添加50个Apple对象
        appleList.add(new Apple("红富士", "青色", 167.9));
        appleList.add(new Apple("红富士", "青色", 122.5));
        appleList.add(new Apple("红富士", "青色", 93.7));
        appleList.add(new Apple("红富士", "青色", 192.3));
        appleList.add(new Apple("瑞雪", "红色", 101.2));
        appleList.add(new Apple("瑞雪", "红色", 155.8));
        appleList.add(new Apple("瑞雪", "红色", 138.4));
        appleList.add(new Apple("瑞雪", "红色", 85.6));
        appleList.add(new Apple("红富士", "红色", 112.7));
        appleList.add(new Apple("红富士", "红色", 98.5));
        appleList.add(new Apple("红富士", "红色", 156.3));
        appleList.add(new Apple("红富士", "红色", 189.1));
        appleList.add(new Apple("红富士", "红色", 132.4));
        appleList.add(new Apple("红星", "红色", 179.8));
        appleList.add(new Apple("红星", "绿色", 103.5));
        appleList.add(new Apple("红星", "绿色", 147.2));
        appleList.add(new Apple("红星", "绿色", 88.7));
        appleList.add(new Apple("红星", "绿色", 166.9));
        appleList.add(new Apple("红星", "青色", 133.1));
        appleList.add(new Apple("红星", "青色", 115.4));
        appleList.add(new Apple("红星", "青色", 191.2));
        appleList.add(new Apple("红星", "青色", 99.6));
        appleList.add(new Apple("国光", "红色", 108.3));
        appleList.add(new Apple("国光", "红色", 159.7));
        appleList.add(new Apple("国光", "红色", 136.2));
        appleList.add(new Apple("国光", "红色", 87.5));
        appleList.add(new Apple("国光", "绿色", 172.4));
        appleList.add(new Apple("国光", "绿色", 121.8));
        appleList.add(new Apple("国光", "绿色", 149.3));
        appleList.add(new Apple("国光", "绿色", 95.9));
        appleList.add(new Apple("国光", "青色", 183.6));
        appleList.add(new Apple("国光", "青色", 118.7));
        appleList.add(new Apple("红富士", "绿色", 105.6));
        appleList.add(new Apple("红富士", "绿色", 176.2));
        appleList.add(new Apple("红富士", "绿色", 89.3));
        appleList.add(new Apple("红富士", "绿色", 145.8));
        appleList.add(new Apple("瑞雪", "绿色", 177.3));
        appleList.add(new Apple("瑞雪", "绿色", 112.9));
        appleList.add(new Apple("瑞雪", "绿色", 143.2));
        appleList.add(new Apple("瑞雪", "绿色", 97.8));
        appleList.add(new Apple("瑞雪", "青色", 188.5));
        appleList.add(new Apple("瑞雪", "青色", 126.4));
        appleList.add(new Apple("瑞雪", "青色", 109.1));
        appleList.add(new Apple("瑞雪", "青色", 163.7));
        appleList.add(new Apple("红星", "红色", 92.4));
        appleList.add(new Apple("红星", "红色", 151.6));
        appleList.add(new Apple("红星", "红色", 128.3));
        appleList.add(new Apple("国光", "青色", 164.2));
        appleList.add(new Apple("国光", "青色", 104.5));

        return appleList;
    }
}
